<?php
namespace app\admin\model;

use think\Model;


class Order extends Model
{

	/**
        * 获取认投金额列表
        * @param $where 条件
        * @param $field 查询字段
        * @author cpy(1442435897@qq.com)
        * @return  array
        */
        public function selectInfo($where,$field = '*') {
            $res = $this->where($where)->field($field)->select();
            return empty($res)?array():$res->toArray();
        }
    
    /**
     * 获取认投金额列表
     * @param $where 条件
     * @param $startTime 开始时间
     * @param $endTime 结束时间
     * @param $limit 分页
     * @param $field 查询字段
     * @author cpy(1442435897@qq.com)
     * @return  array
     */
    public function memberRecruitDetails($where,$startTime,$endTime,$sort = 'a.id desc',$field = '*') {
        return $this->alias('a')->field($field)->join('tf_member b','b.id = a.uid','LEFT')->join('tf_project c','c.id = a.project_id','LEFT')->where($where)
                    ->whereTime('create_times','between',[$startTime,$endTime])->order($sort)->paginate('', false, page_param());
    }

    /**
     * 获取认投项目
     * @param $where 条件
     * @param $startTime 开始时间
     * @param $endTime 结束时间
     * @param $limit 分页
     * @param $field 查询字段
     * @author cpy(1442435897@qq.com)
     * @return  array
     */
    public function memberRecruitProject($where,$field = '*') {
        $res = $this->alias('a')->field($field)->join('tf_project c','c.id = a.project_id','LEFT')->where($where)->order('c.create_time desc')->select();
        return empty($res)?array():$res->toArray();
    }

    /**
     * 获取认投金额列表
     * @param $condition 条件
     * @param $field 查询字段
     * @param $page 分页数据
     * @param $page 排序
     * @author hey_heyang
     * @return  array()
     */

    public function getOrderMoneyData($condition,$field,$page=10,$sort='ASC'){
        $data = $this->alias('o')->join('member m','m.id=o.uid')->join('project p','p.id=o.project_id')->where($condition)->field($field)->order('o.create_time'.$sort)->paginate($page);
        return $data;

    }
    /**
     * 获取认投金额列表
     * @param $condition 条件
     * @param $field 查询字段
     * @author hey_heyang
     * @return  array()
     */

    public function getOrderMoneyDataToArray($condition,$field){
        $data = $this->alias('o')->join('member m','m.id=o.uid')->join('project p','p.id=o.project_id')->where($condition)->field($field)->select()->toArray();
        return $data;

    }
    /**
     * 获取平台新增的代付记录
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getAddAnotherMoney($const_start_time,$end_time){
        $model = new AddAnotherMoneyInfo();
        $data=$model->where(['user_id'=>0,'status'=>1,'type'=>1,'payment_time'=>array('between',[$const_start_time,$end_time+1])])->sum('money'); //未在系统记录的代付总金额
        return $data;
    }

    /**
     * 获取系统中代付的记录
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getPayForMember($const_start_time,$end_time){
        $model= new OrderPayment();
        $data= $model->where(['status'=>2,'payment_time'=>array('between',[$const_start_time,$end_time+1])])->sum('money');
        return $data;
    }

    /**
     * 获取系统中未代付的记录
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getNotDfMoney($const_start_time,$end_time){
        $data= $this->where(['status'=>2,'payment_status'=>0,'create_times'=>array('between',[date('Y-m-d H:i:s',$const_start_time),date('Y-m-d H:i:s',$end_time+1)])])->sum('money');
        return $data;
    }




    /**
     * 获取系统中认投的记录
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getOrderMember($const_start_time,$end_time){
        $data=$this->where(['status'=>array('in','2,3'),'type'=>1,'create_time'=>array('between',[$const_start_time,$end_time+1])])->sum('money');
        return $data;
    }

    /**
     * 获取系统提取手续费
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getPtfeeTotal($const_start_time,$end_time){
        $model = new AddAnotherMoneyInfo();
        $data=$model->where(['status'=>1,'type'=>2,'payment_time'=>array('between',[$const_start_time,$end_time+1])])->sum('money'); //平台提取的手续费
        return $data;
    }


    /**
     * 获取提现手续费
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getTxfeeTotal($const_start_time,$end_time){
        $model = new GetCash();
        $data=$model->where(['status'=>2,'date'=>array('between',[date('Y-m-d H:i:s',$const_start_time),date('Y-m-d H:i:s',$end_time+1)])])->sum('fee'); // 提现手续费
        return $data;
    }

    /**
     * 获取充值手续费
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getCzfeeTotal($const_start_time,$end_time){
        $model = new Recharge();
        $data=$model->where(['status'=>2,'date'=>array('between',[date('Y-m-d H:i:s',$const_start_time),date('Y-m-d H:i:s',$end_time+1)])])->sum('fee'); //充值手续费
        return $data;
    }

    /**
     * 获取平台充值的推广金
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getPtChargeTotal($const_start_time,$end_time){
        $model = new AddAnotherMoneyInfo();
        $data=$model->where(['status'=>1,'type'=>3,'payment_time'=>array('between',[$const_start_time,$end_time+1])])->sum('money');  //平台充值的推广金
        return $data;
    }

    /**
     * 获取平台计算的推广账户集金额
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getJsMoney($const_start_time,$end_time){
        $model = new AddAnotherMoneyInfo();
        $data=$model->where(['status'=>1,'type'=>4,'payment_time'=>array('between',[$const_start_time,$end_time+1])])->sum('money');  //平台结算的金额
        return $data;
    }

    /**
     * 获取直推佣金
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getMemberChargeTotal($const_start_time,$end_time){
        $model = new OrderProfit();
        $data=$model->where(['status'=>1,'create_time'=>array('between',[$const_start_time,$end_time+1])])->sum('money'); //直推佣金总额
        return $data;
    }

    /**
     * 获取用户总金额
     * @param $end_time 时间条件
     * @author hey_heyang
     * @return  array()
     */
    public function getMemberMoneyTotal($end_time){
        $member_log_model = new MemberMoneyTotalLog();
        $member_model = new Member();
        $now_day = date('Y-m-d',time());
        if(date('Y-m-d',$end_time)==$now_day){
            $data=$member_model->sum('money');
        }else{
            $data=$member_log_model->where(array('deadline_time'=>$end_time))->sum('money');
        }

        return $data;
    }

    /**
     * 获取已关联的订单
     * @param  $id 关联的用款申请ID
     * @author hey_heyang(635870453@qq.com)
     * @return array()
     */
    public function getHasBindOrder($id){
        $field= 'or.*,pr.project_name,pu.company_name,m.username,m.mobile';
        $res = $this->alias('or')->join('project pr','or.project_id=pr.id')
            ->join('project_user pu','pr.initiator=pu.id')
            ->join('member m','or.uid=m.id')
            ->where(array('or.relevance_id'=>$id))
            ->field($field)
            ->order('or.money asc,or.payment_status asc')
            ->paginate('', false, page_param());
        $res_list = empty($res)?array():$res->toArray();
        $res_list['origin_data'] = $res;
        return $res_list;
    }

    /**
     * 获取与代付金额匹配的订单列表
     * @param  $id 认投项目id
     * @param  $money 代付金额
     * @author hey_heyang(635870453@qq.com)
     * @return array()
     */
    public function getMostOrder($id,$money){
        $field= 'or.*,pr.project_name,pu.company_name,m.username,m.mobile';
        $res = $this->alias('or')->join('project pr','or.project_id=pr.id')
            ->join('project_user pu','pr.initiator=pu.id')
            ->join('member m','or.uid=m.id')
            ->where(array('or.project_id'=>$id,'or.status'=>array('gt',1),'or.type'=>1,'or.relevance_id'=>0,'or.series_number'=>0,'payment_status'=>0))
            ->field($field)
            ->order('or.money asc')
            ->select();
        $res_list = empty($res)?array():$res->toArray();
        $target_list=array();
        $current_money =0;
        $orderids='';
        if(count($res_list)){
            foreach ($res_list as $key=>$value){
                 if(($current_money+$value['money'])<=$money){
                     $current_money+=floatval($value['money']);
                     $target_list[] =$value;
                     $orderids = empty($orderids)?$value['id']:($orderids.','.$value['id']);
                 }else{
                    break;
                 }
            }
            if(!empty($orderids) && $current_money>0){ //存储筛选出的订单id字符串
                $_SESSION['orderids'] = $orderids;
                $where['or.id'] =array('in',$orderids);
                $now_res = $this->alias('or')->join('project pr','or.project_id=pr.id')
                    ->join('project_user pu','pr.initiator=pu.id')
                    ->join('member m','or.uid=m.id')
                    ->where($where)
                    ->field($field)
                    ->order('or.money desc')
                    ->paginate('', false, page_param());
            }else{
                $now_res = $this->alias('or')->join('project pr','or.project_id=pr.id')
                    ->join('project_user pu','pr.initiator=pu.id')
                    ->join('member m','or.uid=m.id')
                    ->where(array('or.id'=>array('lt',0)))
                    ->field($field)
                    ->order('or.money asc')
                    ->paginate('', false, page_param());   //建立空的分页对象
            }
        }else{
            $now_res = $this->alias('or')->join('project pr','or.project_id=pr.id')
                ->join('project_user pu','pr.initiator=pu.id')
                ->join('member m','or.uid=m.id')
                ->where(array('or.id'=>array('lt',0)))
                ->field($field)
                ->order('or.money asc')
                ->paginate('', false, page_param());   //建立空的分页对象
        }
        return array('origin_list'=>$now_res,'order_list'=>$target_list,'total_money'=>$current_money,'orderids'=>$orderids);

    }

}